package com.st.controller.background;

import com.st.entity.ProductCategory;
import com.st.service.IProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductCategoryController {
    @Autowired
    private IProductCategoryService productCategoryService;

    @RequestMapping("/query")
    public ModelAndView query(ProductCategory productCategory) {
        ModelAndView mv = new ModelAndView();
        List<ProductCategory> list = productCategoryService.query(productCategory);
        mv.addObject("list", list);
        mv.setViewName("/background/productCategory");
        return mv;
    }

    @RequestMapping("add")
    @ResponseBody
    public String add(ProductCategory productCategory) {
        int count = productCategoryService.add(productCategory);
        if (count == 1) {
            return "1";
        } else {
            return "0";
        }
    }

    @RequestMapping("toUpdate")
    public ModelAndView toUpdate(ProductCategory productCategory) {
        ModelAndView mv = new ModelAndView();
        List<ProductCategory> list = productCategoryService.query(productCategory);
        mv.addObject("list", list.get(0));
        mv.setViewName("/background/product_up");
        return mv;
    }

    @RequestMapping("update")
    @ResponseBody
    public String update(ProductCategory productCategory) {
        int count = productCategoryService.update(productCategory);
        if (count == 1) {
            return "1";
        } else {
            return "0";
        }
    }
}
